package service;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import search.SearchWordBean;

import common.main.MainServlet;

public class SearchWordAutoCompleteServlet extends MainServlet
{

	public void init(ServletConfig config) throws ServletException
	{

	}
	
	public void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, Exception 
	{
		response.setCharacterEncoding("utf-8");

		String sw = entoKo(request.getParameter("sw"));
		
		
		
		inputTable.put("sw", sw);
		SearchWordBean swb = new SearchWordBean();
		swb.setDataTable(inputTable);
		swb.funcExecute();
		
		ArrayList swList = swb.getDataList();

		if (swList.size() > 0)
		{
			PrintWriter out = response.getWriter();
			response.setContentType("text/xml");
			out.println("<response>");
			for(int i=0; i<swList.size(); i++)
			{
				Hashtable swListTable = (Hashtable)swList.get(i);
				String word = (String) swListTable.get("WORD");
				out.println("<WORD>" + word + "</WORD>");
			}
			out.println("</response>");
			out.close();
		}
		else
		{
			response.setStatus(HttpServletResponse.SC_NO_CONTENT);
		}
	}

	
	protected String entoKo(String en)
	{
		String enKo = null;
		try
		{
			enKo = new String(en.getBytes("8859_1"),"KSC5601");
		}
		catch(UnsupportedEncodingException e)
		{
			System.out.println(e);
		}

		return enKo;
	}
	
}
